//
//  ONISOModel.m
//
//  Created by   on 6/5/14
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "ONISOModel.h"
#import "ONISOObjects.h"


NSString *const kONISOModelObjects = @"objects";
NSString *const kONISOModelTotalObjects = @"totalObjects";


@interface ONISOModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ONISOModel

@synthesize objects = _objects;
@synthesize totalObjects = _totalObjects;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
    NSObject *receivedONISOObjects = [dict objectForKey:kONISOModelObjects];
    NSMutableArray *parsedONISOObjects = [NSMutableArray array];
    if ([receivedONISOObjects isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedONISOObjects) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedONISOObjects addObject:[ONISOObjects modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedONISOObjects isKindOfClass:[NSDictionary class]]) {
       [parsedONISOObjects addObject:[ONISOObjects modelObjectWithDictionary:(NSDictionary *)receivedONISOObjects]];
    }

    self.objects = [NSArray arrayWithArray:parsedONISOObjects];
            self.totalObjects = [[self objectOrNilForKey:kONISOModelTotalObjects fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    NSMutableArray *tempArrayForObjects = [NSMutableArray array];
    for (NSObject *subArrayObject in self.objects) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForObjects addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForObjects addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForObjects] forKey:kONISOModelObjects];
    [mutableDict setValue:[NSNumber numberWithDouble:self.totalObjects] forKey:kONISOModelTotalObjects];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.objects = [aDecoder decodeObjectForKey:kONISOModelObjects];
    self.totalObjects = [aDecoder decodeDoubleForKey:kONISOModelTotalObjects];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_objects forKey:kONISOModelObjects];
    [aCoder encodeDouble:_totalObjects forKey:kONISOModelTotalObjects];
}

- (id)copyWithZone:(NSZone *)zone
{
    ONISOModel *copy = [[ONISOModel alloc] init];
    
    if (copy) {

        copy.objects = [self.objects copyWithZone:zone];
        copy.totalObjects = self.totalObjects;
    }
    
    return copy;
}


@end
